package com.uwo.house.controller;

import com.uwo.house.beans.request.PackageQueryReq;
import com.uwo.house.beans.request.PackageReq;
import com.uwo.house.beans.response.MaterialDictionary;
import com.uwo.house.beans.response.PackageDetailsResp;
import com.uwo.house.beans.response.PackageListResp;
import com.uwo.house.service.IPackageManagerService;
import com.uwo.house.util.DateUtils;
import com.uwo.house.util.UtilReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by cyn1c on 2017/7/23.
 */
@Controller
@RequestMapping("/package")
public class PackageManagerController {
    @Autowired
    public IPackageManagerService packageManagerService;

     /**
     * 发布套餐信息
     *
     * @param packageReq
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map addPackage(@RequestBody PackageReq packageReq) {
        int result = this.packageManagerService.addPackage(packageReq);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "发布成功");
        }
        return UtilReturn.resErrorMap("发布失败",UtilReturn.ERROR_CODE);
    }

    /**
     * 修改套餐信息
     *
     * @param packageReq
     * @return
     */
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public Map modifyPackage(@RequestBody PackageReq packageReq) {
        int result = this.packageManagerService.modifyPackage(packageReq);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "修改成功");
        }
        return UtilReturn.resErrorMap("修改失败",UtilReturn.ERROR_CODE);
    }


    /**
     * 管理中心-套餐查询
     *
     * @param packageId
     * @param decorationStyle
     * @param distributeDateFrom
     * @param distributeDateTo
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Map getPackageList(@RequestParam(value = "packageId", required = false) String packageId,
                              @RequestParam(value = "decorationStyle", required = false) String decorationStyle,
                              @RequestParam(value = "distributeDateFrom", required = false) String distributeDateFrom,
                              @RequestParam(value = "distributeDateTo", required = false) String distributeDateTo) {
        PackageQueryReq packageQueryReq = new PackageQueryReq();
        packageQueryReq.setPackageId(packageId);
        packageQueryReq.setDecorationStyle(decorationStyle);
        packageQueryReq.setDistributeDateFrom(DateUtils.getDate(distributeDateFrom));
        packageQueryReq.setDistributeDateTo(DateUtils.getDate(distributeDateTo));
        List<PackageListResp> list = this.packageManagerService.getPackageList(packageQueryReq);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 根据id获取套餐详情 （管理端和客户端公用）
     *
     * @param packageId
     * @return
     */
    @RequestMapping(value = "/details", method = RequestMethod.GET)
    @ResponseBody
    public Map getPackageDetailsById(@RequestParam("packageId") String packageId) {
        PackageDetailsResp packageDetailsResp = this.packageManagerService.getPackageDetailsById(packageId);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, packageDetailsResp);
    }

    /**
     * 管理中心-删除套餐
     *
     * @param packageId
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public Map deletePackage(@RequestParam("packageId") String packageId) {
        int result = this.packageManagerService.deletePackage(packageId);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "删除成功");
        }
        return UtilReturn.resErrorMap("删除失败",UtilReturn.ERROR_CODE);
    }

    /**
     * 发布-获取明细字典数据
     *
     * @return
     */
    @RequestMapping(value = "/dictionary", method = RequestMethod.GET)
    @ResponseBody
    public Map getMaterialDictionary() {
        List<MaterialDictionary> list = this.packageManagerService.getMaterialDictionary();
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 验证合同编号是否存在
     * @param packageId
     * @return
     */
    @RequestMapping(value = "/exist", method = RequestMethod.GET)
    @ResponseBody
    public Map checkIdExist(@RequestParam("packageId") String packageId) {
        int result = this.packageManagerService.checkIdExist(packageId);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.ERROR_CODE, UtilReturn.SUCCESS_MESSAGE, "套餐编号已存在");
        }
        return UtilReturn.resErrorMap("套餐编号不存在",UtilReturn.ERROR_CODE);
    }

}
